package com.home.job.zh2.job2;


import java.util.UUID;

public class Item {
    public String id;
    public String name;
    public double price;
    public double discountPrice;
    public int discountQuantity;

    public Item() {
    }

    // 设置折扣
    public Item(String name, double price, double discountPrice, int discountQuantity) {
        this.id = UUID.randomUUID().toString();
        this.name = name;
        this.price = price;
        this.discountPrice = discountPrice;
        this.discountQuantity = discountQuantity;
    }

    // 无折扣
    public Item(String name, double price) {
        this.id = UUID.randomUUID().toString();
        this.name = name;
        this.price = price;
        this.discountPrice = 0;
        this.discountQuantity = 0;
    }

    @Override
    public String toString() {
        String ss = "物品名称 = " + name  + ", 单价 = " + price;
        if (this.discountQuantity > 0) {
            ss += ", 折后价 = " + discountPrice + ", 享受折扣数量 = " + discountQuantity;
        } else {
            ss += ", 无折扣";
        }

        return ss;
    }

    public double getTotalPrice (int number) {
        double price = 0.0;
        if (this.discountQuantity > 0 && number > this.discountQuantity) {
            // 享受折扣
            price += Math.floor((number / this.discountQuantity)) * discountPrice;
            price += (number % this.discountQuantity) * this.price;
        } else {
            price += number * this.price;
        }
        return price;
    }
}
